<template>
  <el-dialog
    :title=" addModel ? '新增项目' : '编辑基础信息'"
    :visible.sync="formVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    top="5vh"
    width="60%"
    @close="closeDialog()"
  >
    <el-form ref="form" v-loading="loading" :model="form" :rules="rules" :label-width="labelWidth" size="small">
      <el-row>
        <el-row>
          <el-col
            :span="24"
          >
            <el-form-item
              label="所属部门"
              prop="fcontrolunitid"
            >
              <treeselect
                v-model="form.fcontrolunitid"
                :options="deptOptions"
                :normalizer="normalizer"
                placeholder="选择所属部门"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="cflastname">
              <el-input v-model.trim="form.cflastname" auto-complete="off" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编码" prop="fnumber">
              <el-input v-model.trim="form.fnumber" auto-complete="off" placeholder="请输入编码" :disabled="!addModel" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="fstatedate">
              <el-date-picker
                v-model="form.fstatedate"
                align="right"
                type="date"
                placeholder="请选择日期"
                value-format="yyyy-MM-dd"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目阶段" prop="cfprojectstage">
              <el-select v-model="form.cfprojectstage" placeholder="请选择项目阶段" style="width: 100%">
                <el-option
                  v-for="item in projectStages"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="fisenabled">
              <el-switch
                v-model="form.fisenabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
          <el-form-item label="是否叶子结点" prop="fisleaf">
            <el-switch
              v-model="form.fisleaf"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
        </el-col> -->
          <el-col :span="12">
            <el-form-item label="序号" prop="fsortno">
              <el-input v-model.number="form.fsortno" auto-complete="off" placeholder="请输入序号" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="地址" prop="fprojectaddress">
              <el-input v-model="form.fprojectaddress" type="textarea" maxlength="200" show-word-limit :row="3" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-row></el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listDept } from '@/api/system/dept'
import { saveOrModify } from '@/api/baseManage/project'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: 'AddOrUpdate',
  components: { Treeselect },
  props: {
    visible: { type: Boolean, default: false },
    labelWidth: { type: String, default: '150px' },
    info: { type: Object, default: _ => {} }
  },
  data() {
    return {
      form: {
        fid: '',
        cflastname: '',
        fnumber: '',
        fstatedate: '',
        cfprojectstage: '',
        fisenabled: '',
        fisleaf: '',
        fprojectaddress: '',
        fsortno: 0,
        fcontrolunitid: ''
      },
      formVisible: false,
      dataSourceVisible: false,
      currentDataSource: {},
      loading: false,
      rules: {
        cflastname: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        fcontrolunitid: [
          { required: true, message: '请选择所属部门', trigger: 'blur' }
        ],
        fnumber: [
          { required: true, message: '请输入编码', trigger: 'blur' }
        ],
        cfprojectstage: [
          { required: true, message: '请选择项目阶段', trigger: 'blur' }
        ]
      },
      projectStages: [{ label: '在建', value: 1 }, { label: '已竣工交付', value: 2 }, { label: '已结算', value: 3 }],
      // 部门树选项
      deptOptions: []
    }
  },
  computed: {
    addModel() {
      return !this.info.fid
    }
  },
  watch: {
    visible(val) {
      if (val) this.getDeptTree()
      this.formVisible = val
      if (val) {
        const info = JSON.parse(JSON.stringify(this.info))
        this.form.fid = info.fid || ''
        this.form.cflastname = info.cflastname || ''
        this.form.fnumber = info.fnumber || ''
        this.form.fstatedate = info.fstatedate || ''
        this.form.cfprojectstage = info.cfprojectstage || (info.fid ? '' : 1)
        this.form.fisenabled = info.fisenabled || 0
        this.form.fisleaf = info.fisleaf || 0
        this.form.fprojectaddress = info.fprojectaddress || ''
        this.form.fcontrolunitid = info.fcontrolunitid || undefined
        this.form.fparentid = info.fparentid
        this.form.fsortno = info.fsortno || 0
      }
    }
  },
  methods: {
    getDeptTree() {
      listDept().then(response => {
        this.deptOptions = this.handleTree(response.data, 'deptId')
      })
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.fid,
        label: node.deptName,
        children: node.children
      }
    },
    /**
     * 关闭窗口
     */
    closeDialog() {
      this.requirements = []
      this.form.fid = ''
      this.$refs.form.resetFields()
      this.$emit('update:visible', false)
    },
    /**
     * 取消操作
     */
    cancel() {
      this.closeDialog()
    },
    /**
     * 确认
     */
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.loading) {
            return
          }
          this.loading = true
          saveOrModify(this.form).then(response => {
            this.$message({ type: 'success', message: '保存成功!' })
            this.$emit('change')
            this.closeDialog()
          }).finally(_ => {
            setTimeout(_ => {
              this.loading = false
            }, 500)
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
